| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818 |
1
453
453
453
453
453
453
1
967
967
967
967
967
967
2
2
967
967
17
967
967
2
2
967
3
3
967
2
2
967
967
967
1
1
1
1
1
1
1
967
967
961
961
961
961
961
961
967
961
1
961
961
2
959
7
961
961
961
1
1
967
931
931
931
967
959
959
959
957
2
2
967
1012
1006
1006
1006
9
9
9
997
997
1006
1006
1006
1006
1006
1006
1006
1006
1006
1006
1006
1006
1006
1006
1006
25
1006
395
93
395
1006
4
1
146
967
125
125
125
125
967
967
967
172
160
160
160
160
160
160
160
160
123
160
3
1
103
103
103
4
99
967
103
967
93
967
11
967
535
967
1012
1012
1012
198
1012
1012
1012
198
198
198
198
72
126
75
198
73
125
72
198
1012
1012
967
2
1
1
967
1
1
1
967
1
961
961
1552
70
1482
1481
1481
1481
1
931
931
1513
1513
62
1451
1451
1451
1451
1
395
93
302
1
390
90
300
967
1
4
4
4
4
1
4
4
4
4
1
1
1
1450
1450
1450
1450
1450
8700
1450
1450
1450
1
1012
1012
1012
132
132
557
557
72
72
251
251
1012
440
572
572
443
443
129
572
1
1012
1012
1012
1012
1012
1012
1012
1011
1012
1012
1012
440
440
117
323
440
440
440
440
1
440
440
200
240
240
108
108
54
54
24
132
132
93
39
24
240
1
440
440
1
1052
1052
836
1
836
267
1052
993
993
1052
993
993
967
1
2172
1
689
453
1
968
451
445
453
75
582
582
6984
582
582
1164
2
582
582
582
582
582
34
34
34
34
1
582
547
545
2
547
5
582
11
10
10
582
12
11
11
582
535
535
582
582
582
582
582
| 'use strict';
angular.module('mgcrea.ngStrap.tooltip', ['mgcrea.ngStrap.helpers.dimensions'])
.provider('$tooltip', function() {
var defaults = this.defaults = {
animation: 'am-fade',
customClass: '',
prefixClass: 'tooltip',
prefixEvent: 'tooltip',
container: false,
target: false,
placement: 'top',
template: 'tooltip/tooltip.tpl.html',
contentTemplate: false,
trigger: 'hover focus',
keyboard: false,
html: false,
show: false,
title: '',
type: '',
delay: 0,
autoClose: false,
bsEnabled: true,
viewport: {
selector: 'body',
padding: 0
}
};
this.$get = function($window, $rootScope, $compile, $q, $templateCache, $http, $animate, $sce, dimensions, $$rAF, $timeout) {
var trim = String.prototype.trim;
var isTouch = 'createTouch' in $window.document;
var htmlReplaceRegExp = /ng-bind="/ig;
var $body = angular.element($window.document);
function TooltipFactory(element, config) {
var $tooltip = {};
// Common vars
var nodeName = element[0].nodeName.toLowerCase();
var options = $tooltip.$options = angular.extend({}, defaults, config);
$tooltip.$promise = fetchTemplate(options.template);
var scope = $tooltip.$scope = options.scope && options.scope.$new() || $rootScope.$new();
if(options.delay && angular.isString(options.delay)) {
var split = options.delay.split(',').map(parseFloat);
options.delay = split.length > 1 ? {show: split[0], hide: split[1]} : split[0];
}
// store $id to identify the triggering element in events
// give priority to options.id, otherwise, try to use
// element id if defined
$tooltip.$id = options.id || element.attr('id') || '';
// Support scope as string options
if(options.title) {
scope.title = $sce.trustAsHtml(options.title);
}
// Provide scope helpers
scope.$setEnabled = function(isEnabled) {
scope.$$postDigest(function() {
$tooltip.setEnabled(isEnabled);
});
};
scope.$hide = function() {
scope.$$postDigest(function() {
$tooltip.hide();
});
};
scope.$show = function() {
scope.$$postDigest(function() {
$tooltip.show();
});
};
scope.$toggle = function() {
scope.$$postDigest(function() {
$tooltip.toggle();
});
};
// Publish isShown as a protected var on scope
$tooltip.$isShown = scope.$isShown = false;
// Private vars
var timeout, hoverState;
// Support contentTemplate option
if(options.contentTemplate) {
$tooltip.$promise = $tooltip.$promise.then(function(template) {
var templateEl = angular.element(template);
return fetchTemplate(options.contentTemplate)
.then(function(contentTemplate) {
var contentEl = findElement('[ng-bind="content"]', templateEl[0]);
Eif(!contentEl.length) contentEl = findElement('[ng-bind="title"]', templateEl[0]);
contentEl.removeAttr('ng-bind').html(contentTemplate);
return templateEl[0].outerHTML;
});
});
}
// Fetch, compile then initialize tooltip
var tipLinker, tipElement, tipTemplate, tipContainer, tipScope;
$tooltip.$promise.then(function(template) {
Iif(angular.isObject(template)) template = template.data;
if(options.html) template = template.replace(htmlReplaceRegExp, 'ng-bind-html="');
template = trim.apply(template);
tipTemplate = template;
tipLinker = $compile(template);
$tooltip.init();
});
$tooltip.init = function() {
// Options: delay
if (options.delay && angular.isNumber(options.delay)) {
options.delay = {
show: options.delay,
hide: options.delay
};
}
// Replace trigger on touch devices ?
// if(isTouch && options.trigger === defaults.trigger) {
// options.trigger.replace(/hover/g, 'click');
// }
// Options : container
Iif(options.container === 'self') {
tipContainer = element;
} else if(angular.isElement(options.container)) {
tipContainer = options.container;
} else if(options.container) {
tipContainer = findElement(options.container);
}
// Options: trigger
bindTriggerEvents();
// Options: target
Iif(options.target) {
options.target = angular.isElement(options.target) ? options.target : findElement(options.target);
}
// Options: show
if(options.show) {
scope.$$postDigest(function() {
options.trigger === 'focus' ? element[0].focus() : $tooltip.show();
});
}
};
$tooltip.destroy = function() {
// Unbind events
unbindTriggerEvents();
// Remove element
destroyTipElement();
// Destroy scope
scope.$destroy();
};
$tooltip.enter = function() {
clearTimeout(timeout);
hoverState = 'in';
if (!options.delay || !options.delay.show) {
return $tooltip.show();
}
timeout = setTimeout(function() {
Eif (hoverState ==='in') $tooltip.show();
}, options.delay.show);
};
$tooltip.show = function() {
if (!options.bsEnabled || $tooltip.$isShown) return;
scope.$emit(options.prefixEvent + '.show.before', $tooltip);
var parent, after;
if (options.container) {
parent = tipContainer;
Iif (tipContainer[0].lastChild) {
after = angular.element(tipContainer[0].lastChild);
} else {
after = null;
}
} else {
parent = null;
after = element;
}
// Hide any existing tipElement
if(tipElement) destroyTipElement();
// Fetch a cloned element linked from template
tipScope = $tooltip.$scope.$new();
tipElement = $tooltip.$element = tipLinker(tipScope, function(clonedElement, scope) {});
// Set the initial positioning. Make the tooltip invisible
// so IE doesn't try to focus on it off screen.
tipElement.css({top: '-9999px', left: '-9999px', right: 'auto', display: 'block', visibility: 'hidden'});
// Options: animation
Eif(options.animation) tipElement.addClass(options.animation);
// Options: type
if(options.type) tipElement.addClass(options.prefixClass + '-' + options.type);
// Options: custom classes
Iif(options.customClass) tipElement.addClass(options.customClass);
// Append the element, without any animations. If we append
// using $animate.enter, some of the animations cause the placement
// to be off due to the transforms.
after ? after.after(tipElement) : parent.prepend(tipElement);
$tooltip.$isShown = scope.$isShown = true;
safeDigest(scope);
// Now, apply placement
$tooltip.$applyPlacement();
// Once placed, animate it.
// Support v1.3+ $animate
// https://github.com/angular/angular.js/commit/bf0f5502b1bbfddc5cdd2f138efd9188b8c652a9
var promise = $animate.enter(tipElement, parent, after, enterAnimateCallback);
Eif(promise && promise.then) promise.then(enterAnimateCallback);
safeDigest(scope);
$$rAF(function () {
// Once the tooltip is placed and the animation starts, make the tooltip visible
Eif(tipElement) tipElement.css({visibility: 'visible'});
});
// Bind events
if(options.keyboard) {
if(options.trigger !== 'focus') {
$tooltip.focus();
}
bindKeyboardEvents();
}
if(options.autoClose) {
bindAutoCloseEvents();
}
};
function enterAnimateCallback() {
scope.$emit(options.prefixEvent + '.show', $tooltip);
}
$tooltip.leave = function() {
clearTimeout(timeout);
hoverState = 'out';
Eif (!options.delay || !options.delay.hide) {
return $tooltip.hide();
}
timeout = setTimeout(function () {
if (hoverState === 'out') {
$tooltip.hide();
}
}, options.delay.hide);
};
var _blur;
var _tipToHide;
$tooltip.hide = function(blur) {
if(!$tooltip.$isShown) return;
scope.$emit(options.prefixEvent + '.hide.before', $tooltip);
// store blur value for leaveAnimateCallback to use
_blur = blur;
// store current tipElement reference to use
// in leaveAnimateCallback
_tipToHide = tipElement;
// Support v1.3+ $animate
// https://github.com/angular/angular.js/commit/bf0f5502b1bbfddc5cdd2f138efd9188b8c652a9
var promise = $animate.leave(tipElement, leaveAnimateCallback);
Eif(promise && promise.then) promise.then(leaveAnimateCallback);
$tooltip.$isShown = scope.$isShown = false;
safeDigest(scope);
// Unbind events
if(options.keyboard && tipElement !== null) {
unbindKeyboardEvents();
}
if(options.autoClose && tipElement !== null) {
unbindAutoCloseEvents();
}
};
function leaveAnimateCallback() {
scope.$emit(options.prefixEvent + '.hide', $tooltip);
// check if current tipElement still references
// the same element when hide was called
Eif (tipElement === _tipToHide) {
// Allow to blur the input when hidden, like when pressing enter key
if(_blur && options.trigger === 'focus') {
return element[0].blur();
}
// clean up child scopes
destroyTipElement();
}
}
$tooltip.toggle = function() {
$tooltip.$isShown ? $tooltip.leave() : $tooltip.enter();
};
$tooltip.focus = function() {
tipElement[0].focus();
};
$tooltip.setEnabled = function(isEnabled) {
options.bsEnabled = isEnabled;
};
$tooltip.setViewport = function(viewport) {
options.viewport = viewport;
};
// Protected methods
$tooltip.$applyPlacement = function() {
Iif(!tipElement) return;
// Determine if we're doing an auto or normal placement
var placement = options.placement,
autoToken = /\s?auto?\s?/i,
autoPlace = autoToken.test(placement);
if (autoPlace) {
placement = placement.replace(autoToken, '') || defaults.placement;
}
// Need to add the position class before we get
// the offsets
tipElement.addClass(options.placement);
// Get the position of the target element
// and the height and width of the tooltip so we can center it.
var elementPosition = getPosition(),
tipWidth = tipElement.prop('offsetWidth'),
tipHeight = tipElement.prop('offsetHeight');
// If we're auto placing, we need to check the positioning
if (autoPlace) {
var originalPlacement = placement;
var container = options.container ? findElement(options.container) : element.parent();
var containerPosition = getPosition(container);
// Determine if the vertical placement
if (originalPlacement.indexOf('bottom') >= 0 && elementPosition.bottom + tipHeight > containerPosition.bottom) {
placement = originalPlacement.replace('bottom', 'top');
} else if (originalPlacement.indexOf('top') >= 0 && elementPosition.top - tipHeight < containerPosition.top) {
placement = originalPlacement.replace('top', 'bottom');
}
// Determine the horizontal placement
// The exotic placements of left and right are opposite of the standard placements. Their arrows are put on the left/right
// and flow in the opposite direction of their placement.
if ((originalPlacement === 'right' || originalPlacement === 'bottom-left' || originalPlacement === 'top-left') &&
elementPosition.right + tipWidth > containerPosition.width) {
placement = originalPlacement === 'right' ? 'left' : placement.replace('left', 'right');
} else if ((originalPlacement === 'left' || originalPlacement === 'bottom-right' || originalPlacement === 'top-right') &&
elementPosition.left - tipWidth < containerPosition.left) {
placement = originalPlacement === 'left' ? 'right' : placement.replace('right', 'left');
}
tipElement.removeClass(originalPlacement).addClass(placement);
}
// Get the tooltip's top and left coordinates to center it with this directive.
var tipPosition = getCalculatedOffset(placement, elementPosition, tipWidth, tipHeight);
applyPlacement(tipPosition, placement);
};
$tooltip.$onKeyUp = function(evt) {
if (evt.which === 27 && $tooltip.$isShown) {
$tooltip.hide();
evt.stopPropagation();
}
};
$tooltip.$onFocusKeyUp = function(evt) {
Eif (evt.which === 27) {
element[0].blur();
evt.stopPropagation();
}
};
$tooltip.$onFocusElementMouseDown = function(evt) {
evt.preventDefault();
evt.stopPropagation();
// Some browsers do not auto-focus buttons (eg. Safari)
$tooltip.$isShown ? element[0].blur() : element[0].focus();
};
// bind/unbind events
function bindTriggerEvents() {
var triggers = options.trigger.split(' ');
angular.forEach(triggers, function(trigger) {
if(trigger === 'click') {
element.on('click', $tooltip.toggle);
} else if(trigger !== 'manual') {
element.on(trigger === 'hover' ? 'mouseenter' : 'focus', $tooltip.enter);
element.on(trigger === 'hover' ? 'mouseleave' : 'blur', $tooltip.leave);
nodeName === 'button' && trigger !== 'hover' && element.on(isTouch ? 'touchstart' : 'mousedown', $tooltip.$onFocusElementMouseDown);
}
});
}
function unbindTriggerEvents() {
var triggers = options.trigger.split(' ');
for (var i = triggers.length; i--;) {
var trigger = triggers[i];
if(trigger === 'click') {
element.off('click', $tooltip.toggle);
} else Eif(trigger !== 'manual') {
element.off(trigger === 'hover' ? 'mouseenter' : 'focus', $tooltip.enter);
element.off(trigger === 'hover' ? 'mouseleave' : 'blur', $tooltip.leave);
nodeName === 'button' && trigger !== 'hover' && element.off(isTouch ? 'touchstart' : 'mousedown', $tooltip.$onFocusElementMouseDown);
}
}
}
function bindKeyboardEvents() {
if(options.trigger !== 'focus') {
tipElement.on('keyup', $tooltip.$onKeyUp);
} else {
element.on('keyup', $tooltip.$onFocusKeyUp);
}
}
function unbindKeyboardEvents() {
if(options.trigger !== 'focus') {
tipElement.off('keyup', $tooltip.$onKeyUp);
} else {
element.off('keyup', $tooltip.$onFocusKeyUp);
}
}
var _autoCloseEventsBinded = false;
function bindAutoCloseEvents() {
// use timeout to hookup the events to prevent
// event bubbling from being processed imediately.
$timeout(function() {
// Stop propagation when clicking inside tooltip
tipElement.on('click', stopEventPropagation);
// Hide when clicking outside tooltip
$body.on('click', $tooltip.hide);
_autoCloseEventsBinded = true;
}, 0, false);
}
function unbindAutoCloseEvents() {
Eif (_autoCloseEventsBinded) {
tipElement.off('click', stopEventPropagation);
$body.off('click', $tooltip.hide);
_autoCloseEventsBinded = false;
}
}
function stopEventPropagation(event) {
event.stopPropagation();
}
// Private methods
function getPosition($element) {
$element = $element || (options.target || element);
var el = $element[0],
isBody = el.tagName === 'BODY';
var elRect = el.getBoundingClientRect();
var rect = {};
// IE8 has issues with angular.extend and using elRect directly.
// By coping the values of elRect into a new object, we can continue to use extend
for (var p in elRect) {
// DO NOT use hasOwnProperty when inspecting the return of getBoundingClientRect.
rect[p] = elRect[p];
}
Iif (rect.width === null) {
// width and height are missing in IE8, so compute them manually; see https://github.com/twbs/bootstrap/issues/14093
rect = angular.extend({}, rect, { width: elRect.right - elRect.left, height: elRect.bottom - elRect.top });
}
var elOffset = isBody ? { top: 0, left: 0 } : dimensions.offset(el),
scroll = { scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.prop('scrollTop') || 0 },
outerDims = isBody ? { width: document.documentElement.clientWidth, height: $window.innerHeight } : null;
return angular.extend({}, rect, scroll, outerDims, elOffset);
}
function getCalculatedOffset(placement, position, actualWidth, actualHeight) {
var offset;
var split = placement.split('-');
switch (split[0]) {
case 'right':
offset = {
top: position.top + position.height / 2 - actualHeight / 2,
left: position.left + position.width
};
break;
case 'bottom':
offset = {
top: position.top + position.height,
left: position.left + position.width / 2 - actualWidth / 2
};
break;
case 'left':
offset = {
top: position.top + position.height / 2 - actualHeight / 2,
left: position.left - actualWidth
};
break;
default:
offset = {
top: position.top - actualHeight,
left: position.left + position.width / 2 - actualWidth / 2
};
break;
}
if(!split[1]) {
return offset;
}
// Add support for corners @todo css
Eif(split[0] === 'top' || split[0] === 'bottom') {
switch (split[1]) {
case 'left':
offset.left = position.left;
break;
case 'right':
offset.left = position.left + position.width - actualWidth;
}
} else if(split[0] === 'left' || split[0] === 'right') {
switch (split[1]) {
case 'top':
offset.top = position.top - actualHeight;
break;
case 'bottom':
offset.top = position.top + position.height;
}
}
return offset;
}
function applyPlacement(offset, placement) {
var tip = tipElement[0],
width = tip.offsetWidth,
height = tip.offsetHeight;
// manually read margins because getBoundingClientRect includes difference
var marginTop = parseInt(dimensions.css(tip, 'margin-top'), 10),
marginLeft = parseInt(dimensions.css(tip, 'margin-left'), 10);
// we must check for NaN for ie 8/9
Iif (isNaN(marginTop)) marginTop = 0;
Iif (isNaN(marginLeft)) marginLeft = 0;
offset.top = offset.top + marginTop;
offset.left = offset.left + marginLeft;
// dimensions setOffset doesn't round pixel values
// so we use setOffset directly with our own function
dimensions.setOffset(tip, angular.extend({
using: function (props) {
tipElement.css({
top: Math.round(props.top) + 'px',
left: Math.round(props.left) + 'px',
right: ''
});
}
}, offset), 0);
// check to see if placing tip in new offset caused the tip to resize itself
var actualWidth = tip.offsetWidth,
actualHeight = tip.offsetHeight;
Iif (placement === 'top' && actualHeight !== height) {
offset.top = offset.top + height - actualHeight;
}
// If it's an exotic placement, exit now instead of
// applying a delta and changing the arrow
if (/top-left|top-right|bottom-left|bottom-right/.test(placement)) return;
var delta = getViewportAdjustedDelta(placement, offset, actualWidth, actualHeight);
if (delta.left) {
offset.left += delta.left;
} else {
offset.top += delta.top;
}
dimensions.setOffset(tip, offset);
Eif (/top|right|bottom|left/.test(placement)) {
var isVertical = /top|bottom/.test(placement),
arrowDelta = isVertical ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight,
arrowOffsetPosition = isVertical ? 'offsetWidth' : 'offsetHeight';
replaceArrow(arrowDelta, tip[arrowOffsetPosition], isVertical);
}
}
function getViewportAdjustedDelta(placement, position, actualWidth, actualHeight) {
var delta = { top: 0, left: 0 },
$viewport = options.viewport && findElement(options.viewport.selector || options.viewport);
if (!$viewport) {
return delta;
}
var viewportPadding = options.viewport && options.viewport.padding || 0,
viewportDimensions = getPosition($viewport);
if (/right|left/.test(placement)) {
var topEdgeOffset = position.top - viewportPadding - viewportDimensions.scroll,
bottomEdgeOffset = position.top + viewportPadding - viewportDimensions.scroll + actualHeight;
if (topEdgeOffset < viewportDimensions.top) { // top overflow
delta.top = viewportDimensions.top - topEdgeOffset;
} else if (bottomEdgeOffset > viewportDimensions.top + viewportDimensions.height) { // bottom overflow
delta.top = viewportDimensions.top + viewportDimensions.height - bottomEdgeOffset;
}
} else {
var leftEdgeOffset = position.left - viewportPadding,
rightEdgeOffset = position.left + viewportPadding + actualWidth;
if (leftEdgeOffset < viewportDimensions.left) { // left overflow
delta.left = viewportDimensions.left - leftEdgeOffset;
} else if (rightEdgeOffset > viewportDimensions.width) { // right overflow
delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset;
}
}
return delta;
}
function replaceArrow(delta, dimension, isHorizontal) {
var $arrow = findElement('.tooltip-arrow, .arrow', tipElement[0]);
$arrow.css(isHorizontal ? 'left' : 'top', 50 * (1 - delta / dimension) + '%')
.css(isHorizontal ? 'top' : 'left', '');
}
function destroyTipElement() {
// Cancel pending callbacks
clearTimeout(timeout);
if($tooltip.$isShown && tipElement !== null) {
if(options.autoClose) {
unbindAutoCloseEvents();
}
if(options.keyboard) {
unbindKeyboardEvents();
}
}
if(tipScope) {
tipScope.$destroy();
tipScope = null;
}
if(tipElement) {
tipElement.remove();
tipElement = $tooltip.$element = null;
}
}
return $tooltip;
}
// Helper functions
function safeDigest(scope) {
scope.$$phase || (scope.$root && scope.$root.$$phase) || scope.$digest();
}
function findElement(query, element) {
return angular.element((element || document).querySelectorAll(query));
}
var fetchPromises = {};
function fetchTemplate(template) {
if(fetchPromises[template]) return fetchPromises[template];
return (fetchPromises[template] = $http.get(template, {cache: $templateCache}).then(function(res) {
return res.data;
}));
}
return TooltipFactory;
};
})
.directive('bsTooltip', function($window, $location, $sce, $tooltip, $$rAF) {
return {
restrict: 'EAC',
scope: true,
link: function postLink(scope, element, attr, transclusion) {
// Directive options
var options = {scope: scope};
angular.forEach(['template', 'contentTemplate', 'placement', 'container', 'delay', 'trigger', 'html', 'animation', 'backdropAnimation', 'type', 'customClass', 'id'], function(key) {
if(angular.isDefined(attr[key])) options[key] = attr[key];
});
// use string regex match boolean attr falsy values, leave truthy values be
var falseValueRegExp = /^(false|0|)$/i;
angular.forEach(['html', 'container'], function(key) {
if(angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key]))
options[key] = false;
});
// should not parse target attribute (anchor tag), only data-target #1454
var dataTarget = element.attr('data-target');
Iif(angular.isDefined(dataTarget)) {
if(falseValueRegExp.test(dataTarget))
options.target = false;
else
options.target = dataTarget;
}
// overwrite inherited title value when no value specified
// fix for angular 1.3.1 531a8de72c439d8ddd064874bf364c00cedabb11
Eif (!scope.hasOwnProperty('title')){
scope.title = '';
}
// Observe scope attributes for change
attr.$observe('title', function(newValue) {
Eif (angular.isDefined(newValue) || !scope.hasOwnProperty('title')) {
var oldValue = scope.title;
scope.title = $sce.trustAsHtml(newValue);
angular.isDefined(oldValue) && $$rAF(function() {
tooltip && tooltip.$applyPlacement();
});
}
});
// Support scope as an object
attr.bsTooltip && scope.$watch(attr.bsTooltip, function(newValue, oldValue) {
if(angular.isObject(newValue)) {
angular.extend(scope, newValue);
} else {
scope.title = newValue;
}
angular.isDefined(oldValue) && $$rAF(function() {
tooltip && tooltip.$applyPlacement();
});
}, true);
// Visibility binding support
attr.bsShow && scope.$watch(attr.bsShow, function(newValue, oldValue) {
if(!tooltip || !angular.isDefined(newValue)) return;
if(angular.isString(newValue)) newValue = !!newValue.match(/true|,?(tooltip),?/i);
newValue === true ? tooltip.show() : tooltip.hide();
});
// Enabled binding support
attr.bsEnabled && scope.$watch(attr.bsEnabled, function(newValue, oldValue) {
// console.warn('scope.$watch(%s)', attr.bsEnabled, newValue, oldValue);
if(!tooltip || !angular.isDefined(newValue)) return;
if(angular.isString(newValue)) newValue = !!newValue.match(/true|1|,?(tooltip),?/i);
newValue === false ? tooltip.setEnabled(false) : tooltip.setEnabled(true);
});
// Viewport support
attr.viewport && scope.$watch(attr.viewport, function (newValue) {
Iif(!tooltip || !angular.isDefined(newValue)) return;
tooltip.setViewport(newValue);
});
// Initialize popover
var tooltip = $tooltip(element, options);
// Garbage collection
scope.$on('$destroy', function() {
Eif(tooltip) tooltip.destroy();
options = null;
tooltip = null;
});
}
};
});
|